package com.example.controller;

import com.example.common.Result;
import com.example.entity.Discuss;
import com.example.service.DiscussService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/discuss")
public class DiscussController {

    @Resource
    private DiscussService discussService;

    /**
     * 添加评论
     */
    // 修改后的评论添加接口
    @PostMapping("/addComment")  // 换个路径名试试
    public Result addDiscuss(@RequestBody Discuss discuss) {
        discussService.addDiscuss(discuss);
        return Result.success();
    }

    /**
     * 根据帖子ID查询评论
     */
    @GetMapping("/getByPostId/comment/{nid}")
    public Result getCommentsByPostId(@PathVariable Long nid) {
        List<Map<String, Object>> comments = discussService.getCommentsByPostId(nid);
        return Result.success(comments);
    }


    /**
     * 更新评论内容
     */
    @PutMapping("/update/{id}")
    public Result updateDiscuss(@PathVariable Long id, @RequestBody Discuss discuss) {
        discuss.setDiscussId(id);
        discussService.updateDiscuss(discuss);
        return Result.success("评论更新成功");
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteDiscuss(@PathVariable Long id) {
        discussService.deleteDiscuss(id);
        return Result.success("评论删除成功");
    }

    /**
     * 查询所有评论
     */
    @GetMapping("/list")
    public Result getAllComments() {
        List<Discuss> comments = discussService.getAllComments();
        return Result.success(comments);
    }
    /**
     * 分页查询评论
     */
    @GetMapping("/selectPage")
    public Result selectPage(Discuss discuss,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Discuss> page = discussService.selectPage(discuss, pageNum, pageSize);
        return Result.success(page);
    }


}
